NBIS ID: 1234
Report Version: 1.0
Request by: Name name (somebody@email.com)
Principal Investigator: Name name (somebody@email.com)
Organisation: Some University
NBIS Staff: Name name (somebody@nbis.se)
## LIBRARIES
# load the packages you need for this document
# data handling
library(dplyr)
library(tidyr)
library(stringr)
# tables
library(kableExtra) # complete table
library(formattable) # table with conditional formatting
library(DT) # interactive table
# graphics
library(ggplot2) # static graphics
# interactive graphics
library(highcharter)
library(plotly)
library(ggiraph) # convert ggplot to interactive
library(dygraphs) # time series
library(networkD3) # network graph
library(leaflet) # interactive maps
library(crosstalk) # linking plots.Rmd documents can be rendered as such:Use <div>content</div> for block content and <span>content</span> for inline content.
For paragraphs, use <p>content</p>. And use classes text-left, text-center and text-right to align text left, center and right respectively.
<p class="text-right" style="background-color: aliceblue">This content is aligned right.</p>
<p class="text-center" style="background-color: aliceblue">This content is aligned center.</p>
This content is aligned right.
This content is aligned center.
Content can be organised into columns using pull-left-XX or pull-right-XX. Classes for 30, 40, 50, 60 and 70 have been implemented for left and right. Note that the total width must sum to 100.
<div class="pull-left-50" style="background-color: aliceblue">This content is pulled left.</div>
<div class="pull-right-50 text-center" style="background-color: aliceblue">This content is pulled right and text centered.</div>
<div class="pull-left-30" style="background-color: aliceblue">This content is pulled left.</div>
<div class="pull-right-70" style="background-color: aliceblue">This content is pulled right.</div>
This is an example of nested columns.
<div class="pull-left-30" style="background-color: aliceblue">This column is pulled left.</div>
<div class="pull-right-70" style="background-color: aliceblue">
This column is pulled right.
<div class="pull-left-50" style="background-color: #d0ece7">
Nested left.
</div>
<div class="pull-right-50" style="background-color: #f2d7d5">
Nested right.
</div>
</div>
Nested left.
Nested right.
Rendering of normal text, numbers and symbols.
ABCDEFGHIJKLMNOPQRSTUYWXYZÅÄÖ
abcdefghijklmnopqrstuvwxyzåäö
0123456789
!“#%&/()$@*^~<>-:;,_±|?+=
Headings can be defined as shown below.
## Level 2 heading
### Level 3 heading
#### Level 4 heading
##### Level 5 heading
###### Level 6 heading
Six custom classes are defined for text scaling. This can be defined inside a paragraph (<p>) or <span>.
<span class="largest">Largest text.</span>
<span class="larger">Larger text.</span>
<span class="large">Large text.</span>
Normal text.
<span class="small">Small text.</span>
<span class="smaller">Smaller text.</span>
<span class="smallest">Smallest text.</span>
Largest text.
Larger text.
Large text.
Normal text.
Small text.
Smaller text.
Smallest text.
A horizontal line can be created using three or more * or -.
***
This is __Bold text__ This is Bold text
This is _Italic text_ This is Italic text
~~Strikethrough~~ text Strikethrough text
This is Subscript H~2~O displayed as H2O
This is Superscript 2^10^ displayed as 210
This is a [link](r-project.org) This is a link
An example of footnote reference 1
> This is a block quote. This
> paragraph has two lines.
>
> 1. This is a list inside a block quote.
> 2. Second item.
This is a block quote. This paragraph has two lines.
- This is a list inside a block quote.
- Second item.
Code can be defined inline where `this` looks like this. R code can be executed inline `r Sys.Date()` producing 2023-12-29. Code can also be defined inside code blocks.
``` This is code ```
This is code
R code is executed inside code blocks like this
```{r}
Sys.Date()
```
which shows the code and output.
## [1] "2023-12-29"
The code and results can be hidden by ```{r,echo=FALSE,results='hide'}`.
Here is another example of executed R code with input and output.
This is a hidden code.
This is a long inline code Very very very very very very very very very very very very very very very very long inline code.
Unordered lists are created using asterisks.
Ordered lists are created using numbers.
Using regular markdown.

The dimensions are based on image and/or fill up the entire available space. You can control the dimension as shown below.
{width=30%}
This image above is now 30% of it’s original width.
This image below is 30% size.
<img src="assets/cover.png" style="width:30%;"/>
This image below is 30% size, has shadow and corners rounded.
<img src="assets/cover.png" style="width:30%;" class="fancyimage"/>
R chunks in RMarkdown can be used to control image display size using the arguemnt out.width.
This image below is displayed at a size of 300 pixels.
```{r,out.width=300}
knitr::include_graphics('assets/cover.png')
```
This image below is displayed at a size of 75 pixels.
```{r,out.width=75}
knitr::include_graphics('assets/cover.png')
```
Some examples of rendering equations.
$e^{i\pi} + 1 = 0$
\(e^{i\pi} + 1 = 0\)
$$\frac{E \times X^2 \prod I}{2+7} = 432$$
\[\frac{E \times X^2 \prod I}{2+7} = 432\]
$$\sum_{i=1}^n X_i$$
\[\sum_{i=1}^n X_i\]
$$\int_0^{2\pi} \sin x~dx$$
\[\int_0^{2\pi} \sin x~dx\]
$\left( \sum_{i=1}^{n}{i} \right)^2 = \left( \frac{n(n-1)}{2}\right)^2 = \frac{n^2(n-1)^2}{4}$
\(\left( \sum_{i=1}^{n}{i} \right)^2 = \left( \frac{n(n-1)}{2}\right)^2 = \frac{n^2(n-1)^2}{4}\)
$\begin{eqnarray} X & \sim & \mathrm{N}(0,1)\\ Y & \sim & \chi^2_{n-p}\\ R & \equiv & X/Y \sim t_{n-p} \end{eqnarray}$
\(\begin{eqnarray} X & \sim & \mathrm{N}(0,1)\\ Y & \sim & \chi^2_{n-p}\\ R & \equiv & X/Y \sim t_{n-p} \end{eqnarray}\)
$\begin{eqnarray} P(|X-\mu| > k) & = & P(|X-\mu|^2 > k^2)\\ & \leq & \frac{\mathbb{E}\left[|X-\mu|^2\right]}{k^2}\\ & \leq & \frac{\mathrm{Var}[X]}{k^2} \end{eqnarray}$
\(\begin{eqnarray} P(|X-\mu| > k) & = & P(|X-\mu|^2 > k^2)\\ & \leq & \frac{\mathbb{E}\left[|X-\mu|^2\right]}{k^2}\\ & \leq & \frac{\mathrm{Var}[X]}{k^2} \end{eqnarray}\)
View of the data using paged tables. This is the default output for RMarkdown.
The most simple table using kable from R package knitr.
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
More advanced table using kableExtra and formattable.
iris[c(1:4,51:53,105:108),] %>%
mutate(Sepal.Length=color_bar("lightsteelblue")(Sepal.Length)) %>%
mutate(Sepal.Width=color_tile("white","orange")(Sepal.Width)) %>%
mutate(Species=cell_spec(Species,"html",color="white",bold=T,
background=c("#8dd3c7","#fb8072","#bebada")[factor(.$Species)])) %>%
kable("html",escape=F) %>%
kable_styling(bootstrap_options=c("striped","hover","responsive"),
full_width=F,position="left") %>%
column_spec(5,width="3cm")| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 51 | 7.0 | 3.2 | 4.7 | 1.4 | versicolor |
| 52 | 6.4 | 3.2 | 4.5 | 1.5 | versicolor |
| 53 | 6.9 | 3.1 | 4.9 | 1.5 | versicolor |
| 105 | 6.5 | 3.0 | 5.8 | 2.2 | virginica |
| 106 | 7.6 | 3.0 | 6.6 | 2.1 | virginica |
| 107 | 4.9 | 2.5 | 4.5 | 1.7 | virginica |
| 108 | 7.3 | 2.9 | 6.3 | 1.8 | virginica |
Interactive table using R package DT.
{plot(x=iris$Sepal.Length,y=iris$Sepal.Width,
col=c("coral","steelblue","forestgreen")[iris$Species],
xlab="Sepal Length",ylab="Sepal Width",pch=19)
legend(x=7,y=4.47,legend=c("setosa","versicolor","virginica"),
col=c("coral","steelblue","forestgreen"),pch=19)}R package ggplot2 is one of the most versatile and complete plotting solutions.
iris %>%
ggplot(aes(x=Sepal.Length,y=Sepal.Width,col=Species))+
geom_point(size=2)+
labs(x="Sepal Length",y="Sepal Width")+
theme_report()R package highcharter is a wrapper around javascript library highcharts.
h <- iris %>%
hchart(.,"scatter",hcaes(x="Sepal.Length",y="Sepal.Width",group="Species")) %>%
hc_xAxis(title=list(text="Sepal Length"),crosshair=TRUE) %>%
hc_yAxis(title=list(text="Sepal Width"),crosshair=TRUE) %>%
hc_chart(zoomType="xy",inverted=FALSE) %>%
hc_legend(verticalAlign="top",align="right") %>%
hc_size(height=400)
htmltools::tagList(list(h))plotly also has a function called ggplotly which converts a static ggplot2 object into an interactive plot.
ggiraph is also an R package that can be used to convert a static ggplot2 object into an interactive plot.
p <- ggplot(iris,aes(x=Sepal.Length,y=Petal.Length,colour=Species))+
geom_point_interactive(aes(tooltip=paste0("<b>Petal Length:</b> ",Petal.Length,"\n<b>Sepal Length: </b>",Sepal.Length,"\n<b>Species: </b>",Species)),size=2)+
theme_bw()
tooltip_css <- "background-color:#e7eef3;font-family:Roboto;padding:10px;border-style:solid;border-width:2px;border-color:#125687;border-radius:5px;"
ggiraph(code=print(p),hover_css="cursor:pointer;stroke:black;fill-opacity:0.3",zoom_max=5,tooltip_extra_css=tooltip_css,tooltip_opacity=0.9)R package dygraphs provides R bindings for javascript library dygraphs for time series data.
R package networkD3 allows the use of interactive network graphs from the D3.js javascript library.
R package crosstalk allows crosstalk enabled plotting libraries to be linked. Through the shared ‘key’ variable, data points can be manipulated simultaneously on two independent plots.
shared_quakes <- SharedData$new(quakes[sample(nrow(quakes), 100),])
lf <- leaflet(shared_quakes,height=300) %>%
addTiles(urlTemplate='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') %>%
addMarkers()
py <- plot_ly(shared_quakes,x=~depth,y=~mag,size=~stations,height=300) %>%
add_markers()
htmltools::div(lf,py)<div class="alert alert-success" role="alert">
This is a success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
This is a danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
This is a warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
This is a info alert—check it out!
</div>
This is a success alert—check it out!
This is a danger alert—check it out!
This is a warning alert—check it out!
This is a info alert—check it out!
The code language can be displayed above input code by setting chunk.title=TRUE.
It can be placed above source block with output.
```{r,chunk.title=TRUE}
Sys.Date()
```
Or it can be placed above source block without output.
```{r,chunk.title=TRUE,eval=FALSE}
Sys.Date()
```
Or above output block if the source is hidden. In this case, the title changes to OUTPUT.
```{r,chunk.title=TRUE,echo=FALSE}
Sys.Date()
```
## [1] "2023-12-29"
For instruction, it might be necessary to run some code and print the output as input-source-code. echo=FALSE hides the source code. comment="" hides the ## from print statements. class.output="r" code highlights the output chunk.
```{r,chunk.title=TRUE,echo=FALSE,comment='',class.output='r'}
cat('Sys.Date()')
```
Code can be hidden for interactive display using accordion=TRUE.
```{r,accordion=TRUE}
Sys.Date()
```
The above code creates the button below.
Note that contents inside the accordion will NOT be printed (when minimised) on converting this HTML to PDF. Expanded accordion block will be printed when converting to PDF.
If the accordion block is to contain regular text and code block, then the raw HTML code needs to be used. Below is an example.
<div class="boxy">
**Challenge**
If I create a vector as follows `x <- c(5,3,9,"6")`, what is the "type" of the element in the third position? Is it a number?
<p>
<button class="btn btn-sm btn-collapse collapsed" type="button" data-toggle="collapse" data-target="#task-vectors" aria-expanded="false" aria-controls="task-vectors">
</button>
</p>
<div class="collapse" id="task-vectors">
<div class="card card-body">
```r
x <- c(5,3,9,"6")
typeof(x[3])
```
```
## [1] "character"
```
The third position is not a number, it's a character. In fact, all elements in this vector are characters. If there is a character in a numeric vector, all elements are converted to characters (`typeof(x)`).
</div>
</div>
</div>The above code creates the block below. Note that the variable task-vectors (used in 3 positions above) needs to changed to a unique value for each such block in a document. The code above renders as shown below.
Challenge
If I create a vector as follows x <- c(5,3,9,"6"), what is the “type” of the element in the third position? Is it a number?
Notice how the help text after the code chunk is also inside the accordion.
css: ["default", "my-theme.css"]pagedown::chrome_print("report.html",output="report.pdf")echo=FALSE in code chunks to hide input code.df_print: default.That reference refers to this footnote.↩︎